package com.ruoyi.tcp.messager;

import com.ruoyi.tcp.util.TcpAesUtil;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import lombok.extern.slf4j.Slf4j;

import java.nio.charset.StandardCharsets;
import java.util.Objects;

/**
 * @author 蒋翔 SY100242
 * @date 2024/8/5 8:15
 * @email SY100242@hnlens.com
 */
@Slf4j
public class CustomerMessageEncoder extends MessageToByteEncoder<CustomerProtocol> {

    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, CustomerProtocol o, ByteBuf byteBuf) throws Exception {

//        byte[] src = TcpAesUtil.hex2byte(o);
//        System.out.println("发送"+new String(src, StandardCharsets.UTF_8));
//        byteBuf.writeBytes(src);
        String channelNoStr = TcpAesUtil.getChannelNoStr(o.getChannelNo());
        byte[] channelBytes = channelNoStr.getBytes(StandardCharsets.UTF_8);
        byte[] encryptData = o.getEncryptData();
        byte[] finalSendData = TcpAesUtil.createFinalSendData(encryptData, channelBytes);
        String s = new String(finalSendData, StandardCharsets.UTF_8);
//        System.out.println("发送数据:"+s);
//        log.info("发送数据:{}",s);
        byteBuf.writeBytes(TcpAesUtil.hex2byte(s));

    }


}
